package cn.tedu.charging.device.repository.impl;

import cn.tedu.charging.common.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingStationPO;
import cn.tedu.charging.common.pojo.query.NearStationsQuery;
import cn.tedu.charging.device.mapper.GunMapper;
import cn.tedu.charging.device.repository.DeviceRepository;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class DeviceRepositoryImpl implements DeviceRepository {
    @Resource(name = "redis01")
    private RedisTemplate redisTemplate;
    @Resource
    private GunMapper gunMapper;
    public List<GeoResult> nearStations(NearStationsQuery query) {
        String key = "beijing_stations";
        Point center = new Point(query.getLongitude(),query.getLatitude());
        Distance radius = new Distance(query.getRadius(), RedisGeoCommands.DistanceUnit.KILOMETERS);
        Circle circle = new Circle(center,radius);
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates();
        GeoResults geoResults = redisTemplate.opsForGeo().radius(key, circle, args);

        return geoResults.getContent();
    }

    public String getStationName(Integer stationId) {
        //组织一个缓存查询逻辑 缓存查询 命中则返回 没命中 查数据库
        String stationKey = "charging:station:cache:" + stationId;
        ChargingStationPO po = (ChargingStationPO) redisTemplate.opsForValue().get(stationKey);
        //判断非空
        String stationName = "";
        if(po != null){
            stationName = po.getStationName();
        }
        return stationName;
    }

    public ChargingStationPO getStationById(Integer stationId) {
        String stationKey = "charging:station:cache:" + stationId;
        return (ChargingStationPO) redisTemplate.opsForValue().get(stationKey);
    }

    public List<ChargingGunInfoPO> getGunListByStationId(Integer stationId) {
        return gunMapper.selectByStationId(stationId);
    }

    public Boolean checkGun(Integer gunId) {
        LambdaQueryWrapper<ChargingGunInfoPO> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.select(ChargingGunInfoPO::getGunStatus)
                .eq(ChargingGunInfoPO::getId, gunId);
        ChargingGunInfoPO chargingGunInfoPO = gunMapper.selectOne(lambdaQueryWrapper);
        return chargingGunInfoPO.getGunStatus() == 1;
    }

    public void changeGunStatus(Integer id, Integer status, List<Integer> statusList) {
        UpdateWrapper<ChargingGunInfoPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.in("gun_status", statusList);
        updateWrapper.set("gun_status", status);
        gunMapper.update(null, updateWrapper);
    }
}
